package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeTemplateDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.vo.AttributeListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeStandardVO;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateListItemVO;
import cn.tedu.csmall.product.service.IAttributeService;
import cn.tedu.csmall.product.service.IAttributeTemplateService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "02属性模版")
@Slf4j
@RestController
@RequestMapping("/attribute")
public class AttributeController {

    @Autowired
    private IAttributeService iAttributeService;

    private AttributeController(){
        log.debug("创建控制器：AttributeTemplateController");
    }
    @ApiOperation("添加属性")
    @PostMapping("/add-new")
    private JsonResult<Void> addNew(AttributeAddNewDTO attributeAddNewDTO){
        log.debug("开始处理添加属性的请求; {}",attributeAddNewDTO);
        iAttributeService.addNew(attributeAddNewDTO);
        return JsonResult.ok();
        }

    @ApiOperation("根据id查询属性详情")
    @ApiOperationSupport(order = 310)
    @GetMapping("/{id:[0-9]+}/select")
    public JsonResult<AttributeStandardVO> selectById(@PathVariable Long id){
        AttributeStandardVO attributeStandardVO = iAttributeService.selectById(id);
        return JsonResult.ok(attributeStandardVO);
    }

    @ApiOperation("修改详情")
    @ApiOperationSupport(order = 500)
    @GetMapping("/update")
    public JsonResult<Void> update(AttributeUpdateDTO attributeUpdateDTO){
        iAttributeService.update(attributeUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据属性模板id查询属性详情")
    @ApiOperationSupport(order = 320)
    @GetMapping("/{id:[0-9]+}/select-template")
    public JsonResult<List<AttributeListItemVO>> selectByTemplateId(@PathVariable Long id){
        List<AttributeListItemVO> attributeListItemVOS = iAttributeService.selectByTemplateId(id);
        return JsonResult.ok(attributeListItemVOS);
    }

    @ApiOperation("根据id删除属性")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "属性id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除属性失败，尝试删除的属性的ID无效！")
                                   @PathVariable Long id) {
        log.debug("开始处理【根据id删除相册】的请求，参数：{}", id);
        iAttributeService.delete(id);
        return JsonResult.ok();
    }


}
